<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>短剧平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            padding: 20px;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        .category-list {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-bottom: 20px;
        }
        .category-item {
            padding: 6px 8px;
            background-color: #f0f0f0;
            cursor: pointer;
            transition: all 0.3s;
        }
        .category-item:hover {
            background-color: #e0e0e0;
        }
        .category-item.active {
            background-color: red;
            color: white;
        }
        .playlet-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
        }
        .playlet-item {
            border: 1px solid #eee;
            overflow: hidden;
            transition: transform 0.3s;
        }
        .playlet-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        .playlet-img {
            width: 100%;
            height: 80%;
            object-fit: cover;
        }
        .playlet-info {
            padding: 10px;
            
        }
        .playlet-title {
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 5px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        .playlet-desc {
            display: -webkit-box;
            -webkit-box-orient: vertical;
            overflow: hidden;
            margin-top: 15px;
        }
        .loading {
            text-align: center;
            padding: 20px;
            font-size: 16px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="category-list" id="categoryList">
            <div class="loading">加载分类中...</div>
        </div>
        
        <div class="playlet-list" id="playletList">
            <div class="loading">请选择分类</div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        let currentCategory = null;
        
        const categoryListEl = document.getElementById('categoryList');
        const playletListEl = document.getElementById('playletList');
        
        document.addEventListener('DOMContentLoaded', () => {
            fetchCategories();
        });
        
        // 1. 获取短剧分类数据
        function fetchCategories() {
            axios.get('https://playlet.zonelian.com/api/category/list', {
                params: {
                    page: 1,
                    limit: 15,
                    zlsj: 'zlsj'
                }
            })
            .then(response => {
                if (response.data.code === 200) {
                    renderCategories(response.data.data.data);
                } else {
                    showError('获取分类数据失败');
                }
            })
            .catch(error => {
                console.error('获取分类数据出错:', error);
                showError('获取分类数据出错');

                fetchCategoriesBackup();
            });
        }
        
        function fetchCategoriesBackup() {
            axios.get('https://playlet.zonelian.com:5501/api/category/list', {
                params: {
                    page: 1,
                    limit: 15,
                    zlsj: 'zlsj'
                }
            })
            .then(response => {
                if (response.data.code === 200) {
                    renderCategories(response.data.data.data);
                } else {
                    showError('获取分类数据失败');
                }
            })
            .catch(error => {
                console.error('获取分类数据出错:', error);
                showError('获取分类数据出错');
            });
        }
        
        // 2. 渲染分类数据
        function renderCategories(categories) {
            if (!categories || categories.length === 0) {
                categoryListEl.innerHTML = '<div class="loading">暂无分类数据</div>';
                return;
            }
            
            categoryListEl.innerHTML = '';
            
            categories.forEach(category => {
                const categoryEl = document.createElement('div');
                categoryEl.className = 'category-item';
                categoryEl.textContent = category.name;
                categoryEl.dataset.cid = category.cid;
                categoryEl.dataset.nameEn = category.name_en;
                
                categoryEl.addEventListener('click', () => {
                    // 移除所有active类
                    document.querySelectorAll('.category-item').forEach(item => {
                        item.classList.remove('active');
                    });
                    
                    // 添加active类到当前点击的分类
                    categoryEl.classList.add('active');
                    
                    // 更新当前选中分类
                    currentCategory = {
                        cid: category.cid,
                        name: category.name,
                        name_en: category.name_en
                    };
                    
                    // 获取该分类下的短剧列表
                    fetchPlaylets(category.name_en);
                });
                
                categoryListEl.appendChild(categoryEl);
            });
            
            if (categories.length > 0) {
                const firstCategory = categoryListEl.querySelector('.category-item');
                firstCategory.click();
            }
        }
        
        // 3. 获取短剧列表数据
        function fetchPlaylets(categoryNameEn) {
            playletListEl.innerHTML = '<div class="loading">加载短剧中...</div>';
            
            axios.get('https://playlet.zonelian.com/api/playlet/list', {
                params: {
                    name_en: categoryNameEn,
                    page: 1,
                    limit: 15,
                    zlsj: 'zlsj'
                }
            })
            .then(response => {
                if (response.data.code === 200) {
                    renderPlaylets(response.data.data.data);
                } else {
                    showError('获取短剧列表失败');
                }
            })
            .catch(error => {
                console.error('获取短剧列表出错:', error);
                showError('获取短剧列表出错');
                // 尝试备用地址
                fetchPlayletsBackup(categoryNameEn);
            });
        }
        
        // 备用地址获取短剧列表
        function fetchPlayletsBackup(categoryNameEn) {
            axios.get('https://playlet.zonelian.com:5501/api/playlet/list', {
                params: {
                    name_en: categoryNameEn,
                    page: 1,
                    limit: 15,
                    zlsj: 'zlsj'
                }
            })
            .then(response => {
                if (response.data.code === 200) {
                    renderPlaylets(response.data.data.data);
                } else {
                    showError('获取短剧列表失败');
                }
            })
            .catch(error => {
                console.error('获取短剧列表出错:', error);
                showError('获取短剧列表出错');
            });
        }
        
        // 4. 渲染短剧列表
        function renderPlaylets(playlets) {
            if (!playlets || playlets.length === 0) {
                playletListEl.innerHTML = '<div class="loading">该分类下暂无短剧</div>';
                return;
            }
            
            playletListEl.innerHTML = '';
            
            playlets.forEach(playlet => {
                const playletEl = document.createElement('div');
                playletEl.className = 'playlet-item';
                
                playletEl.innerHTML = `
                    <img src="${playlet.img}" alt="${playlet.title}" class="playlet-img" onerror="this.src='https://via.placeholder.com/200x120?text=图片加载失败'">
                    <div class="playlet-info">
                        <div class="playlet-title">${playlet.title}</div>
                        <div class="playlet-desc">${playlet.description || '暂无描述'}</div>
                    </div>
                `;
                
                playletListEl.appendChild(playletEl);
            });
        }
        
        // 显示错误信息
        function showError(message) {
            const errorEl = document.createElement('div');
            errorEl.className = 'loading';
            errorEl.textContent = message;
            
            if (currentCategory) {
                playletListEl.innerHTML = '';
                playletListEl.appendChild(errorEl);
            } else {
                categoryListEl.innerHTML = '';
                categoryListEl.appendChild(errorEl);
            }
        }
    </script>
</body>
</html>